const db = wx.cloud.database()
const ml = db.collection('musiclist')
const audioCtx = wx.createInnerAudioContext()
Page({
  data:{
    tab:0, //顶部栏选中
    item:0, //切换页面
    playIndex:0, //播放列表
    state:'paused', //控制暂停、播放
    play:{
      currentTime:0, //当前播放时长
      duration:0, //歌曲时长
      percent:0, //播放器进度条
      title:'',  //歌名
      singer:'', //歌手
      coverImg:'' //图片路径
    },
    recommend:[] //储存随机推荐热门歌曲数组
  },
  onLoad(){
    ml.get().then(res=>{ //获取数据库的数据
      console.log(res)
      this.setData({
        musiclist:res.data,
      })
      this.selectMusic(0) 
      this.randomSong();
    })
    audioCtx.onError(()=>{
      console.log('播放失败:'+audioCtx.src)
    })
    audioCtx.onEnded(()=>{
      this.next()
    })
    audioCtx.onCanplay(()=>{
      this.setData({
        'play.duration':audioCtx.duration
      })
      if(this.data.state == 'running'){
        audioCtx.play()
      }
    })
    audioCtx.onTimeUpdate(()=>{
      this.setData({
        'play.duration':audioCtx.duration,
        'play.currentTime':audioCtx.currentTime,
        'play.percent':audioCtx.currentTime/audioCtx.duration * 100
      })
    })
  },
  // click(e){
  //   console.log(e)
  //   audioCtx.src = this.data.musiclist[e.currentTarget.dataset.id].src
  //   audioCtx.play()
  // },
  changItem(e){
    console.log(e)
    this.setData({
      item:e.currentTarget.dataset.item
    })
  },
  changeTab(e){
    console.log(e)
    this.setData({
      tab:e.detail.current
    })
  },
  selectMusic(index){
    console.log('change')
    let music = this.data.musiclist[index]
    audioCtx.src = music.src
    this.setData({
      playIndex:index,
      play:music,
      'play.currentTime':0,
      'play.duration':0,
      'play.percent':0
    })
  },
  play(){
    audioCtx.play()
    this.setData({
      state:'running'
    })
  },
  pause(){
    audioCtx.pause()
    this.setData({
      state:'paused'
    })
  },
  next(){
    let index = (this.data.playIndex >= this.data.musiclist.length-1)?0:this.data.playIndex + 1
    this.selectMusic(index)
    if(this.data.state=='running'){
      this.play()
    }
  },
  change(e){
    console.log(e)
    this.selectMusic(e.currentTarget.dataset.index)
    this.play()
  },
  changePage(e){
    console.log(e)
    this.setData({
      item:e.currentTarget.dataset.page
    })
  },
  sliderChange(e){   //拖动进度条音乐播放跳转
    console.log(e)
    var second = e.detail.value * audioCtx.duration / 100
    audioCtx.seek(second)  //seek跳转
  },
  randomSong(){
    let randomArr = [] //储存随机数数组 
    let music = this.data.musiclist  //获取musiclist的数据
    let recommend = []  //声明recommend
    for (let i = 0;i<9;i++) { 
      let random = Math.floor(Math.random() * music.length); // 按照数据库歌曲数量 取随机整数
      randomArr[i] = random; 
      for (let j = 0; j < i; j++) {
        if (randomArr[i] == randomArr[j]) {
          i--;   //如果有相同的随机数就再重新产生一次
        }
      }
    }
    console.log(randomArr)
    for(let i = 0;i<9;i++){    
      //将随机数组做music下标 赋值给recommend 实现随机生成热门歌曲
       recommend[i] = music[randomArr[i]]
    }
    console.log(recommend)
    this.setData({  
      recommend:recommend
    })
  }
})